﻿@page "/diagrams"

@using Blazor.Diagrams.Core
@using Blazor.Diagrams.Core.Geometry
@using Blazor.Diagrams.Core.Models

<!-- required to resolve DiagramCanvas component -->
@using Blazor.Diagrams.Components
@using Blazor.Diagrams.Core.Options
@using Blazor.Diagrams.Options
@using Blazor.Diagrams

<h1>Z Blazor Diagrams</h1>

<p>This component demonstrates a diagram.</p>

<!--
Parent of DiagramCanvas has to have a fixed width/height
or it will not be rendered.

100vw = 100% viewport width
100vh = 100% viewport height
-->
<div style="width:100vw; height: 100vh">
  <CascadingValue Value="Diagram">
    <DiagramCanvas></DiagramCanvas>
  </CascadingValue>
</div>

@code {
  private BlazorDiagram Diagram { get; set; }

  protected override void OnInitialized()
  {
    base.OnInitialized();

    var options = new BlazorDiagramOptions
    {
      AllowMultiSelection = true, // Whether to allow multi selection using CTRL
      Links = 
      {
      },
      Zoom = 
      {
        Minimum = 0.5, // Minimum zoom value
        Inverse = false, // Whether to inverse the direction of the zoom when using the wheel
      }
    };
    Diagram = new BlazorDiagram(options);
    
    Setup();
  }
  
  private void Setup()
  {
    var node1 = NewNode(50, 50);
    var node2 = NewNode(300, 300);
    var node3 = NewNode(300, 50);
    Diagram.Nodes.Add(new [] { node1, node2, node3 });
    
    Diagram.Links.Add(new LinkModel(node1.GetPort(PortAlignment.Right), node2.GetPort(PortAlignment.Left)));
  }
  
  private static NodeModel NewNode(double x, double y)
  {
    var node = new NodeModel(new Point(x, y));
    node.AddPort(PortAlignment.Bottom);
    node.AddPort(PortAlignment.Top);
    node.AddPort(PortAlignment.Left);
    node.AddPort(PortAlignment.Right);
    return node;
  }
}
